// pages/doctor-goods-add-2/doctor-goods-add-2.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    productid: '', //产品id
    productName: '', // 产品名称
    servicePrice: 0, // 自定义服务费
    totalprice: 0, // 产品总价
    servicetotalprice: 0, // 平台服务费总价
    choosedTap: [], // 已选标签
    goodimage: '', // 缩略图
    choosed: [], // 已选标签文字数组
    choosedid: [], // 已选标签id数组
    servicefees: [], // 服务费项目数据数组
    choosedfeesids: [], // 已选服务费项目id数组
    goodimg: '', // 上传后的图片
    oldinfo: [], // 旧数据
    makepricepercent: '', // 预约金百分比
    placename: '请选择(必选)', // 场地费名字
    devicename: '请选择(选填)', // 设备费名字
    goodsname: '请选择(选填)', // 用品费名字
    placeid: '', // 场地id
    deviceid: '', // 设备id
    goodsid: '', // 用品id
    placeprice: 0, // 场地费
    deviceprice: 0, // 设备费
    goodsprice: 0, // 商品费
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      'productid': options.id,
    })
    this.getOldInfo();
    this.getMakepricePercent(); // 获取预约金百分比
  },

  // 添加产品名称
  addProductName: function(e) {
    var productName = e.detail.value;
    this.setData({
      'productName': productName,
    })
  },

  // 添加自定义服务费
  addServicePrice: function(e) {
    var servicePrice = Number(e.detail.value);
    if (Number.isNaN(servicePrice)){
      wx.showToast({
        title: '请输入正确的金额',
        icon:'none',
        duration:1000
      });
      return;
    }
    var totalprice = servicePrice + Number(this.data.servicetotalprice);
    var makeprice = totalprice * (this.data.makepricepercent);
    if (e.detail.value != ""){
      this.setData({
        'servicePrice': servicePrice.toFixed(2),
        'totalprice': totalprice.toFixed(2),
        'oldinfo.totalPrice': totalprice.toFixed(2),
        'makeprice': makeprice.toFixed(2),
      })
    }else{
      this.setData({
        'servicePrice': 0,
        'totalprice': 0,
        'oldinfo.totalPrice': 0,
        'makeprice': 0,
      })
    }
  },

  // 选择缩略图
  chooseImg: function(e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      count:1,
      success: function(res) {
        if (res.tempFilePaths.length == 1) {
          that.setData({
            'goodimage': res.tempFilePaths[0]
          })
          that.uploadimages(); // 选择图片后 上传到服务器上
        } else {
          wx.showModal({
            title: '提示',
            content: '仅能上传一张图片',
          })
        }
      },
    })
  },

  // 上传图片
  uploadimages: function(e) {
    var that = this;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    });
    var images = this.data.images;
    var imagesArr = [];
    app.uploadFile({
      url: 'doctor/product/uploadFile',
      filePath: that.data.goodimage,
      success: function(res) {
        var path = JSON.parse(res.data).data[0].filePath;
        that.setData({
          'goodimg': path,
        })
      }
    })
  },

  // 下一页
  nextPage: function(e) {
    if (!this.data.goodimg) {
      wx.showToast({
        title: '请添加产品图片',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (!this.data.productName) {
      wx.showToast({
        title: '请添加产品名称',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (!this.data.placeprice) {
      wx.showToast({
        title: '请选择场地使用费',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (!this.data.servicePrice) {
      wx.showToast({
        title: '请添加您的服务费',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    wx.navigateTo({
      url: '../doctor-goods-edit-3/doctor-goods-edit-3?productId=' + this.data.productid + '&productName=' + this.data.productName + '&servicePrice=' + this.data.servicePrice + '&productPic=' + this.data.goodimg + '&totalPrice=' + this.data.totalprice + '&makePrice=' + this.data.makeprice + '&cityType=' + this.data.placeid + '&deviceId=' + this.data.deviceid + '&narcosis=' + this.data.goodsid + '&goodsImages=' + JSON.stringify(this.data.oldinfo.goodsImages) + '&isEdit=1' + '&productDesc=' + this.data.oldinfo.productDesc,
    })
  },

  // 获取现有信息
  getOldInfo: function(e) {
    var that = this;
    app.request({
      url: 'doctor/product/getEditPage',
      data: {
        'productId': that.data.productid,
      },
      success(res) {
        that.setData({
          'oldinfo': res.data,
          'goodimg': res.data.thumbnail,
          'tag': res.data.tag, // 已选分类
          'productName': res.data.goodName,
          'totalprice': res.data.totalPrice, // 总价
          'servicePrice': res.data.servicePrice, // 自定义服务费
          'makeprice': res.data.makePrice, // 预约金
          'servicetotalprice': parseFloat(res.data.totalPrice) - parseFloat(res.data.servicePrice), // 平台费用总价
          
        });

        if (res.data.cityrankName) {
          var name = res.data.cityrankName + ' ￥' + res.data.cityrankPrice;
          that.setData({
            placename: name,
            placeid: res.data.cityrank,
            placeprice: res.data.cityrankPrice,
          })
        }
        if (res.data.deviceName) {
          var name = res.data.deviceName + ' ￥' + res.data.devicePrice;
          that.setData({
            devicename: name,
            deviceid: res.data.device,
            deviceprice: res.data.devicePrice,
          })
        }
        if (res.data.narcosisName) {
          var name = res.data.narcosisName + ' ￥' + res.data.narcosisPrice;
          that.setData({
            goodsname: name,
            goodsid: res.data.narcosis,
            goodsprice: res.data.narcosisPrice,
          })
        }
       
      }
    })
  },

  // 加载预约金百分比
  getMakepricePercent: function(e) {
    var that = this;
    app.request({
      url: 'doctor/product/subscription',
      data: {},
      success: function(res) {
        that.setData({
          makepricepercent: res.data.subscription,
        });
      }
    })
  },
  // 场地使用
  placeMoney(){
    wx.navigateTo({
      url: '../doctor-goods-edit-2-s/doctor-goods-edit-2-s?feeType=1'
    });
    this.setData({
      devicename: '请选择(选填)'
    })
  },
  // 医疗
  iatricalMoney(e){
    wx.navigateTo({
      url: '../doctor-goods-edit-2-s/doctor-goods-edit-2-s?feeType=2&placeid='+this.data.placeid
    });
  }
})